/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managementClient;

import analyticsServer.Event;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Gruppe 95
 */
public class EventBuffer {
    private static final Logger logger = Logger.getLogger("EventBuffer");
    private static final String newLine=System.getProperty("line.separator");
    private ArrayList<Event> events;
    private boolean autoMode = false;
    
    public EventBuffer() {
        events = new ArrayList<Event>(1000);
        logger.setLevel(Level.OFF);
    }
    
    /**
     * Handles incoming event: false if automode, true if event gets buffered.
     * @param e 
     */
    public boolean handleEvent(Event e) {
        if(autoMode) {
            return false;
        }
            logger.log(Level.INFO, "Add new event to buffer: {0}", e.toString());
            events.add(e);
            return true;
    }
    
    /**
     * Prints out all buffered events, empties buffer
     */
    public String printEvents() {
        logger.info("Call printEvents");
        if(events.size() < 1) {
           logger.info("Call printEvents, no events buffered");
            return "Sorry, no events in buffer.";
        }
        StringBuilder output = new StringBuilder();
        for(Event e : events) {
        logger.log(Level.INFO, "Call printEvents, print event: {0}", e.toString());
            output.append(e.toString()).append(newLine);
        }
        events.clear();
        return output.toString();
    }

    /**
     * @param autoMode the autoMode to set
     */
    public void setAutoMode(boolean autoMode) {
        this.autoMode = autoMode;
        if(autoMode)
        {
            events.trimToSize();
        }
        else
        {
            events.ensureCapacity(1000);
        }
    }
    
}
